package 并发编程01.死锁的案例;

import java.awt.*;

/*
*
* @author idea
* @date 2018/7/13
* @des 模拟在多线程编程里面出现的死锁情况
* 这一段代码的主要功能就在于让t1,t2两个线程互相抢占对方的资源，导致双方都没办法获取到相应的锁
*/
public class DeadLock {
    public String locka="a";
    public String lockb="b";

    public static void main(String[] args) {
        new DeadLock().deadLock();

    }

    public void deadLock(){
        Thread t1=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (locka){
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lockb){

                    }
                }
            }
        });
        Thread t2=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockb){
                    synchronized (locka){
                        System.out.println("this is b");
                    }
                }
            }
        });
        t1.start();
        t2.start();
    }
}
